package com.itfreer.data.metadata;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.data.config.ConfigVersionService;
import com.itfreer.data.metadata.dao.DefaultMetadataDao;
import com.itfreer.data.metadata.entity.DataMetadataEntity;
import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.BaseServiceImp;
import com.itfreer.form.api.DataInputResult;
import com.itfreer.form.api.metadate.MetadataEntity;

/**
 * 定义数据结构服务
 */
@Component("DefaultMetadataServiceImp")
public class DefaultMetadataServiceImp extends BaseServiceImp<DataMetadataEntity> implements DefaultMetadataService {

	@Autowired(required = false)
	private DefaultMetadataDao metadataDao;
	@Autowired(required = false)
	private ConfigVersionService configVersionService;

	private Map<String, List<MetadataEntity>> cache = new HashMap<>();

	@Override
	protected BaseDao<DataMetadataEntity> getDao() {
		return metadataDao;
	}

	/**
	 * 是否为租户间共享实体对象
	 */
	protected boolean isShareTenant() {
		return true;
	}

	/**
	 * 获取租户间共享字段名称 用于SAAS下数据管理
	 */
	protected String getShareTenantFieldName() {
		return "shareTenantID";
	}
	
	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 */
	@Override
	public DataMetadataEntity add(DataMetadataEntity entity) {
		cache.clear();
		configVersionService.addVersion();
		return super.add(entity);
	}

	/**
	 * 更新实体
	 * 
	 * @param guid
	 *            唯一值
	 * @param entity
	 *            实体
	 * @return
	 */
	@Override
	public DataMetadataEntity update(DataMetadataEntity entity) {
		cache.clear();
		configVersionService.addVersion();
		return super.update(entity);
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@Override
	public Boolean delete(String guid) {
		cache.clear();
		configVersionService.addVersion();
		return super.delete(guid);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@Override
	public Boolean deletes(String guids) {
		cache.clear();
		configVersionService.addVersion();
		return super.deletes(guids);
	}

	@Override
	public List<MetadataEntity> getEntitys(String metaData) {
		if (cache.containsKey(metaData)) {
			return cache.get(metaData);
		}

		Map<String, Object> where = new HashMap<String, Object>();
		where.put("mid", metaData);
		Map<String, Integer> order = new HashMap<String, Integer>();
		order.put("order", 0);
		List<DataMetadataEntity> list = super.getEntitys(where, order);

		List<MetadataEntity> metadataEntitys = new ArrayList<>();
		for (DataMetadataEntity entity : list) {
			MetadataEntity toEntity = new MetadataEntity();
			BeanUtils.copyProperties(entity, toEntity);
			metadataEntitys.add(toEntity);
		}

		cache.put(metaData, metadataEntitys);
		return cache.get(metaData);
	}

	/**
	 * 导入Excel数据
	 * 
	 * @param fileNmae
	 *            文件名
	 * @param fileStream
	 *            数据文件
	 * @param inputID
	 *            导入配置
	 * @param startrow
	 *            开始导入行
	 * @return
	 */
	@Override
	public DataInputResult input(String fileName, InputStream fileStream, String inputID, Integer startrow) {
		configVersionService.addVersion();
		return super.input(fileName, fileStream, inputID, startrow);
	}

	/**
	 * 导入Shape数据
	 * 
	 * @param fileNmae
	 *            文件名
	 * @return
	 */
	@Override
	public DataInputResult importShape(String fileName, String inputID) {
		configVersionService.addVersion();
		return super.importShape(fileName, inputID);
	}
}